package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.ts.api.http.org.entity.apply.OrgApplyLongYanQiNuoShanDongHeZe;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import java.util.Base64;

import static com.gitee.apanlh.web.http.HttpContentType.APPLICATION_JSON;

/**
 * 安心分期-龙岩市企诺API全量15 山东菏泽
 * <a href="https://www.yuque.com/liefoxbaijie/crm/fd7bi625tgyb4ele?singleDoc#">...</a>
 *
 * @author 李源聪
 */
@ApiPlan(productId = "423", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5LongYanQiNuoShanDongHeZe implements ApiOrgPhoneMd5ConvertHandler {

    private static final String KEY = "1Xt7DfC0bFruQXK0yambZQ==";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        apiDataExchangeParameter.setHttpContentType(APPLICATION_JSON);
        return JsonUtils.toJson(MapUtils.newHashMap(newMap -> {
            newMap.put("channel", "127.0.0.1");
            newMap.put("phoneMd5", apiDataExchangeParameter.getPhoneMd5());
        }));
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        OrgApplyLongYanQiNuoShanDongHeZe orgApplyLongYanQiNuoShanDongHeZe = new OrgApplyLongYanQiNuoShanDongHeZe();
        orgApplyLongYanQiNuoShanDongHeZe.setName(apiApplyParameter.getUserName());
        orgApplyLongYanQiNuoShanDongHeZe.setPhone(apiApplyParameter.getPhone());
        orgApplyLongYanQiNuoShanDongHeZe.setFolderId("7");
        orgApplyLongYanQiNuoShanDongHeZe.setSource("融客通");
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        orgApplyLongYanQiNuoShanDongHeZe.setOtherInformation(apiFieldConverter.getExtendInfo());
        byte[] encode = Base64.getDecoder().decode(KEY);
        AES ecb = AES.createEcb(encode);
        String encrypt = ecb.encryptToBase64Str(JsonUtils.toJson(orgApplyLongYanQiNuoShanDongHeZe));
        return JsonUtils.toJson(MapUtils.newHashMap(newMap -> newMap.put("encryptedThreeStr", encrypt)));
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
